package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.dao.GoodsDetailPictureMapper;
import com.jiangjiang.eshop.commodity.domain.GoodsDetailPictureEntity;
import com.jiangjiang.eshop.common.constant.PathType;
import com.jiangjiang.eshop.common.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GoodsDetailPictureService
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/2 17:51
 * @Version 1.0
 **/
@Service
public class GoodsDetailPictureService extends ServiceImpl<GoodsDetailPictureMapper, GoodsDetailPictureEntity> {


    @Value("${commodity.goods.image.upload.detail.picture.path.type}")
    private String uploadDirPathType;
    @Value("${commodity.goods.image.upload.detail.picture.path}")
    private String uploadDirPath;

    /**
     * 根据明细id删除明细数据
     *
     * @param detailId 明细id
     */
    public void removeByDetailId(Long detailId) {
        LambdaQueryWrapper<GoodsDetailPictureEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsDetailPictureEntity::getGoodsDetailId, detailId);
        this.remove(wrapper);
    }

    /**
     * @param goodsDetailId
     * @param pictures
     * @return
     */
    public List<String> batchUpload(Long goodsDetailId, MultipartFile[] pictures) throws Exception {

        List<String> pictureUrls = new ArrayList<>();
        for (MultipartFile picture : pictures) {
            String pictureUrl = uploadPicture(picture);
            pictureUrls.add(pictureUrl);
        }

        if (CollectionUtils.isNotEmpty(pictureUrls)) {
            List<GoodsDetailPictureEntity> detailPictures = pictureUrls.stream()
                    .map(url -> GoodsDetailPictureEntity.builder()
                            .goodsDetailId(goodsDetailId)
                            .picturePath(url)
                            .build()).collect(Collectors.toList());
            this.saveBatch(detailPictures);
        }
        return pictureUrls;
    }

    /**
     * 上传图片数据
     *
     * @param picture 图片
     * @return 图片的绝对路径
     * @throws Exception
     */
    private String uploadPicture(MultipartFile picture) throws Exception {
        String realUploadDirPath = getRealUploadDirPath();
        return FileUtils.uploadFile(picture, realUploadDirPath);
    }

    /**
     * 获取最终图片的上传路径
     *
     * @return
     */
    private String getRealUploadDirPath() throws Exception {
        String realUploadDirPath = uploadDirPath;
        if (PathType.RELATIVE.equals(uploadDirPathType)) {
            realUploadDirPath = FileUtils.getPathByRelative(uploadDirPath);
        }
        return realUploadDirPath;
    }


    /**
     * 根据detail id 批量删除图片
     * @param goodsDetailId 明细id
     * @return 处理结果
     */
    public Boolean batchRemoveByGoodsDetailId(Long goodsDetailId) {
        LambdaQueryWrapper<GoodsDetailPictureEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsDetailPictureEntity::getGoodsDetailId, goodsDetailId);
        return this.remove(wrapper);
    }
}
